import turtle
import random
points = []

y = 200
for i in range(5):
    points.append([0, y])
    y-=20

# points = [[0,0], [0,-20], [0, -40],[0, -60], [0, -80],
#           [0,-100], [0, -120], [0, -140],[0, -160], [0, -180],[0,-200], [0, -220], [0, -240],[0, -260], [0, -280],
#           [0,-300], [0, -320], [0, -340],[0, -360], [0, -380]]
index = 1
step = -20
def up_goto(pos):
    turtle.up()
    turtle.goto(pos)
    turtle.down()


def move():
    turtle.clear()
    for i in range(len(points)):
        point = points[i]
        up_goto(point)
        turtle.dot(20, 'red')
    points.pop(0)
    new_point = points[-1].copy()
    new_point[index] += step
    points.append(new_point)
    food()
    turtle.ontimer(move, 300)


def func_up():
    global index, step
    index = 1
    step = 20

def func_down():
    global index, step
    index = 1
    step = -20

def func_left():
    global index, step
    index = 0
    step = -20

def func_right():
    global index, step
    index = 0
    step = 20

def food():
    x = random.randint(-340, 340)
    y = random.randint(-340, 340)
    up_goto((x,y))
    turtle.dot(20, 'cyan')
turtle.tracer(False)
move()
turtle.onkeypress(func_up, 'Up')
turtle.onkeypress(func_down, 'Down')
turtle.onkeypress(func_left, 'Left')
turtle.onkeypress(func_right, 'Right')
turtle.listen()
turtle.done()

